<!DOCTYPE html>
<html>

<head>
    <meta charset="UTF-8">
    <title>人事管理系统</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport"
        content="width=device-width,user-scalable=yes, minimum-scale=0.4, initial-scale=0.8,target-densitydpi=low-dpi" />
    <link rel="shortcut icon" href="/favicon.ico" type="image/x-icon" />
        <link rel="stylesheet" href="../../static/css/font.css">
    <link rel="stylesheet" href="../../static/css/xadmin.css">
     <script src="../../static/js/jquery-1.12.4.min.js" charset="utf-8"></script>
    <script type="text/javascript" src="../../static/lib/layui/layui.js" charset="utf-8"></script>
    <script type="text/javascript" src="../../static/js/xadmin.js"></script>
</head>

<body>
    <div class="x-body layui-anim layui-anim-up">
        <form class="layui-form">
			<input type="hidden" name='filename' id='filename'>
            <div class="layui-form-item">
                <label class="layui-form-label">
                    <span class="x-red">*</span>标题
                </label>
                <div class="layui-input-inline">
                    <input type="text" id="title" name="title" lay-verify="required" 
                        autocomplete="off" class="layui-input">
                </div>
              
            </div>

			 <div class="layui-form-item">
            	<label class="layui-form-label">
                    <span class="x-red">*</span>描述
                </label>
	        </div>
	        <div class="layui-form-item">
	 		<textarea id="demo" style="display: none;" name="content"></textarea>
	 		</div>
			<div class="layui-upload layui-form-item">
			  <button type="button" class="layui-btn layui-btn-normal" id="test8">选择文件</button>
			  <button type="button" class="layui-btn" id="test9">开始上传</button>
			</div>
            <div class="layui-form-item">
                <label for="L_repass" class="layui-form-label">
                </label>
                <button class="layui-btn" lay-filter="add" lay-submit="">
                    &nbsp;&nbsp;添加&nbsp;&nbsp;
                </button>
            </div>
        </form>
    </div>
    <script>
        layui.use(['form', 'layer','layedit','upload'], function () {
            $ = layui.jquery;
            var form = layui.form
                , layer = layui.layer
                ,upload = layui.upload;
            var layedit = layui.layedit;
            var myLayedit = layedit.build('demo', {
          	  tool: ['strong' //加粗
          		  ,'italic' //斜体
          		  ,'underline' //下划线
          		  ,'del' //删除线
          		  ,'|',,'left', 'center', 'right', '|', 'face']
          }); //建立编辑器

          
        //选完文件后不自动上传
          upload.render({
            elem: '#test8'
            ,url: '../../upload.action' //改成您自己的上传接口
            ,auto: false
            ,exts: 'txt|docx|doc' 
            ,accept: 'file' //普通文件
            //,multiple: true
            ,bindAction: '#test9'
            ,before: function(obj){
            		layer.msg('文件上传中...', {
            		icon: 16,
            		shade: 0.01,
            		time: 0
            		})
            	}
            ,done: function(res){
              layer.msg('上传成功');
              $('#filename').val(res.url);
              console.log(res.url)
            }
          });
          
            //监听提交
            form.on('submit(add)', function (data) {
            	var content = layedit.getContent(myLayedit);
            	var title = $('#title').val();
            	var filename = $('#filename').val();
                //发异步，把数据提交,ajax 代码
                $.ajax({
                    url: '../../addDocument.action',
                    type: 'post',
                    data: {content,title,filename},
                    success: function (data) {
                        if (data == 1) {
                            layer.alert(
                                "增加成功", { icon: 6 },
                                function () {
                                    // 获得frame索引
                                    var index = parent.layer.getFrameIndex(window.name);
                                    //关闭当前frame
                                    parent.layer.close(index);
                                    //修改成功后刷新父界面
                                    window.parent.location.reload();
                            });
                        }
                        else {
                        	 layer.alert("增加信息失败！", { icon: 2 })
                        }
                    },
                    error: function (e) {
                        layer.alert("error！提交失败！")
                    }
                });
                return false;
            });


        });
    </script>

</body>

</html>